<?php
/**
 * Created by PhpStorm.
 * User: mr.hao
 * Date: 2018/7/28
 * Time: 17:15
 */

namespace wechat\bun;

use wechat\Wx;

class Fangfa extends Wx {


//    定义一个方法，专门写回复文本
    public function text($content){

//        $time = time();

//        %s起占位符的作用
        $xml = '<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>';
//        $xml = "<xml><ToUserName><![CDATA[$this->message->FromUserName]]></ToUserName><FromUserName><![CDATA[$this->message->ToUserName]]></FromUserName><CreateTime></CreateTime>$time<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[$content]]></Content></xml>";
//        $xml = <<<php
//<xml><ToUserName><![CDATA[$this->message->FromUserName]]></ToUserName><FromUserName><![CDATA[$this->message->ToUserName]]></FromUserName><CreateTime></CreateTime>$time<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[$content]]></Content></xml>";
//php;
//        sprintf会依次进行替换
        $xml = sprintf($xml,$this->message->FromUserName,$this->message->ToUserName,time(),$content);
//        $xml = sprintf($xml,$this->message->FromUserName,$this->message->ToUserName,time(),$content);
        echo $xml;

//        完成测试回复文本
    }


//    测试回复图片信息
    public function image($image){


        $xml = '<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image></xml>';
//        $xml = '<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image></xml>';

        $xml = sprintf($xml,$this->message->FromUserName,$this->message->ToUserName,time(),$image);
        echo $xml;

//        测试成功
    }

//    封装被关注时回复的消息
    public function isGuanzhu(){
        return $this->message->MsgType=='event' && $this->message->Event=='subscribe';
    }



//    封装取消关注时进行相应的操作
    public function unGuanzhu(){
        return $this->message->MsgType=='event' && $this->message->Event=='unsubscribe';
    }


}
